/*    */ package com.yc.upload;
/*    */ 
/*    */ import com.yc.dao.FileDAO;
/*    */ import java.io.BufferedInputStream;
/*    */ import java.io.BufferedOutputStream;
/*    */ import java.io.File;
/*    */ import java.io.FileInputStream;
/*    */ import java.io.IOException;
/*    */ import java.io.PrintWriter;
/*    */ import javax.servlet.ServletException;
/*    */ import javax.servlet.ServletOutputStream;
/*    */ import javax.servlet.http.HttpServlet;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */ import javax.servlet.http.HttpSession;
/*    */ import org.apache.commons.logging.Log;
/*    */ import org.apache.commons.logging.LogFactory;
/*    */ 
/*    */ public class DownLoadServlet extends HttpServlet
/*    */ {
/* 21 */   private static final Log log = LogFactory.getLog(DownLoadServlet.class);
/*    */ 
/* 23 */   FileDAO fileDao = null;
/*    */ 
/*    */   public void init() {
/* 26 */     this.fileDao = new FileDAO();
/*    */   }
/*    */ 
/*    */   public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
/*    */   {
/* 31 */     Object objUser = request.getSession().getAttribute("stxtUName");
/* 32 */     if (objUser == null) {
/* 33 */       response.getWriter().print("failed");
/* 34 */       log.equals("User name is null and you cant download the file . ");
/* 35 */       return;
/*    */     }
/* 37 */     String strFileID = request.getParameter("strDownFileID");
/* 38 */     String strFileName = this.fileDao.getFilePath(strFileID);
/* 39 */     log.info("Down file name is : [" + strFileName + "]");
/*    */ 
/* 41 */     String shortName = strFileName.substring(strFileName.lastIndexOf("\\") + 1);
/* 42 */     shortName = new String(shortName.getBytes("gbk"), "iso8859-1");
/* 43 */     response.setContentType("application/x-msdownload");
/*    */ 
/* 46 */     boolean IEVersion6_0 = request.getHeader("User-Agent").indexOf("MSIE 6.0") > 0;
/* 47 */     response.setHeader("Content-Disposition", "attachment;filename=" + shortName);
/* 48 */     if (IEVersion6_0) {
/* 49 */       response.setHeader("Content-Disposition", "filename=" + shortName);
/*    */     }
/*    */     try
/*    */     {
/* 53 */       ServletOutputStream os = response.getOutputStream();
/* 54 */       BufferedOutputStream bout = new BufferedOutputStream(os);
/* 55 */       File file = new File(strFileName);
/* 56 */       FileInputStream fis = new FileInputStream(file);
/* 57 */       BufferedInputStream bin = new BufferedInputStream(fis);
/* 58 */       while (bin.available() > 0)
/*    */       {
/* 60 */         byte[] b = new byte[512];
/* 61 */         int nResult = bin.read(b);
/* 62 */         if (nResult == -1)
/*    */           break;
/* 64 */         bout.write(b, 0, nResult);
/*    */       }
/* 66 */       fis.close();
/* 67 */       bout.flush();
/* 68 */       os.flush();
/* 69 */       os.close();
/* 70 */       response.setStatus(200);
/* 71 */       response.flushBuffer();
/*    */     } catch (IOException e) {
/* 73 */       e.printStackTrace();
/*    */     }
/*    */   }
/*    */ 
/*    */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*    */     throws ServletException, IOException
/*    */   {
/* 80 */     doPost(request, response);
/*    */   }
/*    */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.yc.upload.DownLoadServlet
 * JD-Core Version:    0.6.2
 */